package com.example.lbstest.controller;

import com.example.lbstest.impl.StudentMapperServiceImpl;
import com.example.lbstest.impl.TeacherMapperServiceImpl;
import com.example.lbstest.pojo.Student;
import com.example.lbstest.pojo.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    TeacherMapperServiceImpl service;

    @RequestMapping("/changePassword")
    @ResponseBody
    public String changePassword(@RequestParam("number")String number, @RequestParam("password")String password){
        int rr = service.changePassword(number,password);
        if(rr == 1){
            System.out.println("----修改密码成功-----");
            return "success";
        }
        return "fail";
    }

    @RequestMapping("/selectOneTeacher")
    @ResponseBody
    public Teacher selectOneTeacher(@RequestParam("teacher_id")String teacher_id){
        Teacher teacher = service.selectOneTeacher(teacher_id);
        if(teacher != null){
            System.out.println("------查询教师成功--------");
        }
        return teacher;
    }

    @RequestMapping("/login")
    @ResponseBody
    public String login(HttpServletRequest request, HttpServletResponse response,
                         HttpSession session, Model model) {

        String number = request.getParameter("number");
        String password = request.getParameter("password");
        Teacher teacher = service.login(number, password);
        if (teacher != null) {
            System.out.println("----登录成功-----");
            return teacher.getName();
        }
        return "fail";
    }
}
